<route lang="yaml">
  meta:
    enabled: false
  </route>

<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus'
import { reactive, ref } from 'vue'
import SharePermissionForm from './form/sharePermissionForm.vue'

interface RowVO {
  id: number
  name: string
}

const tableData = ref<RowVO[]>([
  { id: 10001, name: '超级管理员' },
  { id: 10002, name: '普通管理员' },
  { id: 10003, name: '销售主管' },
  { id: 10004, name: '销售' },
])

const searchform = reactive({
  name: '',
  phone: '',
})

const roleFormModal = ref(false)
const sharePermissionFormListRef = ref()
const roleFormTitle = ref<string>('添加角色')
const roleFormRef = ref<FormInstance>()
const roleForm = ref({
  id: null,
  name: '',
  remark: '',
})

function search() {
  console.log('搜索')
}
function addHandle() {
  console.log(6666)
  roleFormModal.value = true
  roleFormTitle.value = '添加角色'
}

function editHandle(row) {
  console.log(row)
  roleFormTitle.value = '修改角色'
  roleFormModal.value = true
  roleForm.value = row
}
function delHandle(id) {
  console.log(id)
}
function shareHandle(id) {
  console.log('分配权限')
  sharePermissionFormListRef.value.openSharePermissionForm(id, 'add')
}

// 添加角色

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: '请输入角色名', trigger: 'blur' },
    { min: 2, max: 15, message: '角色名称字符数在2~15个字符', trigger: 'blur' },
  ],
})

function resetForm() {
  roleForm.value = {
    id: null,
    name: '',
    remark: '',
  }
}

function submitForm(formEl: FormInstance | undefined) {

}

function closeUserForm(formEl: FormInstance | undefined) {
  resetForm()
  roleFormModal.value = false
}
</script>

<template>
  <div>
    <FaPageMain>
      <el-form :model="searchform" label-width="auto" :inline="true" style="min-width: 600px">
        <el-form-item label="角色名" prop="name">
          <el-input v-model="searchform.name" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="background-color: #409EFF; border-color: #409EFF;" :icon="Search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" :icon="Plus" @click="addHandle">
            新增角色
          </el-button>
        </el-form-item>
      </el-form>
    </FaPageMain>
    <FaPageMain>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID" width="100" />
        <el-table-column prop="name" label="角色名" width="120" />
        <el-table-column prop="id" label="操作" width="300">
          <template #default="scope">
            <ElButton type="primary" circle @click="editHandle(scope.row)">
              <template #icon>
                <FaIcon name="i-ep:edit" />
              </template>
            </ElButton>
            <ElButton type="danger" circle @click="delHandle(scope.row.id)">
              <template #icon>
                <FaIcon name="i-ep:delete" />
              </template>
            </ElButton>
            <ElButton type="warning" circle @click="shareHandle(scope.row.id)">
              <template #icon>
                <FaIcon name="i-ep:operation" />
              </template>
            </ElButton>
          </template>
        </el-table-column>
      </el-table>
    </FaPageMain>
    <FaModal v-model="roleFormModal" :title="roleFormTitle" :align-center="true" @close="closeUserForm(roleFormRef)">
      <div class="flex-start-center flex-wrap gap-2">
        <el-form ref="roleFormRef" :model="roleForm" :rules="rules" label-width="auto" style="width: 100%">
          <el-form-item label="角色名" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="描述" prop="remark">
            <el-input v-model="roleForm.remark" :rows="2" type="textarea" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button @click="resetForm()">
          重置
        </el-button>
        <el-button type="primary" @click="submitForm(roleFormRef)">
          提交
        </el-button>
      </template>
    </FaModal>
    <SharePermissionForm ref="sharePermissionFormListRef" />
  </div>
</template>
